package com.googlecode.android_scripting.facade;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.googlecode.android_scripting.facade.FacadeManager;
import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
import com.googlecode.android_scripting.rpc.Rpc;
import com.googlecode.android_scripting.rpc.RpcParameter;

public class MapsforgeFacade extends RpcReceiver {
	  /**
	   * An instance of this interface is passed to the facade. From this object, the resource IDs can
	   * be obtained.
	   */
	  public interface Resources {
	    int getLogo48();
	  }

	  private final Service mService;
	  private final Handler mHandler;

	  @Override
	  public void shutdown() {
	  }

	  public MapsforgeFacade(FacadeManager manager) {
	    super(manager);
	    mService = manager.getService();
	    mHandler = new Handler(mService.getMainLooper());
	  }
	  
	  // Brute force trial!!
	  @Rpc(description = "Displays a map! hell yeah!")
	  public void showMapsforgeMap(@RpcParameter(name = "message") final String message) {
	    mHandler.post(new Runnable() {
	      public void run() {
	        Toast.makeText(mService, message, Toast.LENGTH_SHORT).show();
	        Log.d("TESTING", "launching erdapfel! with: MapFile "+message);	        
	        PackageManager pm = mService.getPackageManager();	        
			Intent intent = pm.getLaunchIntentForPackage("edu.fib.upc.erdapfel");			
			intent.putExtra("MapFile", message);
			
			mService.startActivity(intent);
	      }
	    });
	  }
	  
	  @Rpc(description = "Displays a kml! hell yeah!")
	  public void showMapsforgeKML(@RpcParameter(name = "message") final String message) {
	    mHandler.post(new Runnable() {
	      public void run() {
	        Toast.makeText(mService, message, Toast.LENGTH_SHORT).show();
	        Log.d("TESTING", "launching erdapfel! with: MapFile "+message);	        
	        PackageManager pm = mService.getPackageManager();	        
			Intent intent = pm.getLaunchIntentForPackage("edu.fib.upc.erdapfel");			
			intent.putExtra("KMLFile", message);
			
			mService.startActivity(intent);
	      }
	    });
	  }
}
